package com.itheima;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;


/**
 * @version 1.0
 * @description 说明
 * @package com.itheima
 */
@SpringBootApplication
@EnableAsync
@MapperScan(basePackages = "com.itheima.mapper")
@EnableAspectJAutoProxy
public class AopLogApp {

    public static void main(String[] args) {
        ConfigurableApplicationContext app = SpringApplication.run(AopLogApp.class, args);

    }

    @Bean(value = "myThreadPoolExecutor")
    public TaskExecutor myThreadPoolExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // 设置核心线程数
        // 业务分类:
        // 1.密级类型：加密、压缩 核心数+1
        // 2.IO型：读写包含网络传输 核心数*2
        executor.setCorePoolSize(20);
        // 设置最大线程数
        executor.setMaxPoolSize(40);
        // 设置队列容量
        executor.setQueueCapacity(200);
        // 设置线程活跃时间（秒）
        executor.setKeepAliveSeconds(60);
        // 设置默认线程名称
        executor.setThreadNamePrefix("sys-oper-");
        // 设置拒绝策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 等待所有任务结束后再关闭线程池
        executor.setWaitForTasksToCompleteOnShutdown(true);

        executor.initialize();

        return executor;
    }
}
